package com.zbw.weightlibrary.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.zbw.weightlibrary.Connector.IBleControl;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import com.zbw.weightlibrary.Connector.NewWeight;
import com.zbw.weightlibrary.Connector.WeightData;
import com.zbw.weightlibrary.R;

public class UsbToSeriMainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener, IinfoCallback {
    private IBleControl IControl;
    public Spinner PL2303HXD_BaudRate_spinner;
    private TextView tv_weight, tvTitle, tv_connectResult;
    private Button btRead, ButTest, ButComfirm;
    private String[] baudrates;//波特率
    private Spinner spBaudrate;
    private int selectBaudrate;//选中波特率
    private ProgressBar progress_bar;
    private RelativeLayout rela_back;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usb_to_seri_main);
        // 隐藏系统通知栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //控件初始化
        initUI();
        initListener();
    }

    public void onResume() {
        super.onResume();
    }

    private void initUI() {
        PL2303HXD_BaudRate_spinner = (Spinner) findViewById(R.id.sp_baudrate);
        tv_weight = (TextView) findViewById(R.id.tv_weight);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText("USB转串口电子秤测试");
        tv_connectResult = (TextView) findViewById(R.id.tv_connectResult);
        ButTest = (Button) findViewById(R.id.ButTest);
        btRead = (Button) findViewById(R.id.ButGetWeight);
        ButComfirm = (Button) findViewById(R.id.ButComfirm);
        progress_bar = (ProgressBar) findViewById(R.id.progress_bar);

        //波特率数据初始化
        baudrates = this.getResources().getStringArray(R.array.baudrate);
        spBaudrate = (Spinner) findViewById(R.id.sp_baudrate);
        ArrayAdapter portAdapter = new ArrayAdapter(this, R.layout.sp_tv_item, this.getResources().getStringArray(R.array.baudrate));
        spBaudrate.setAdapter(portAdapter);

        rela_back = (RelativeLayout) findViewById(R.id.rela_back);
        rela_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initListener() {

        ButTest.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                if (progress_bar.getVisibility() == View.GONE) {
                    progress_bar.setVisibility(View.VISIBLE);
                }
                WeightData weightData = new WeightData();
                weightData.setBaudrate(selectBaudrate);
                IControl = NewWeight.getInstance().NewControl(NewWeight.WEIGHTUSBTOSERIAL, UsbToSeriMainActivity.this, UsbToSeriMainActivity.this, weightData);
                IControl.bleConnect();
            }
        });


        btRead.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
//                readData();
                if (IControl != null) {
                    IControl.getWeight();
                } else {
                    Toast.makeText(UsbToSeriMainActivity.this, "请先初始化！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        ButComfirm.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                if (IControl != null) {
                    //保存波特率和端口号数据
                    SaveData();
                    IControl.bleDisconnect();
                } else {
                    Toast.makeText(UsbToSeriMainActivity.this, "请先初始化！", Toast.LENGTH_SHORT).show();
                }
                finish();
            }
        });

        //波特率选择
        spBaudrate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //保存当前选择的波特率
                selectBaudrate = Integer.parseInt(baudrates[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

    }

    private void SaveData() {
        Intent intent = this.getIntent();
        intent.putExtra("mBaudrateUsbSerial", selectBaudrate + "");
        this.setResult(-1, intent);
    }


    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onConnect(String str, final boolean is) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progress_bar.getVisibility() == View.VISIBLE) {
                    progress_bar.setVisibility(View.GONE);
                }
                if (is) {
                    tv_connectResult.setText("连接设备成功");
                } else {
                    tv_connectResult.setText("连接设备失败");
                }
            }
        });
    }

    @Override
    public void onDisconnect(String str, boolean is) {

    }

    @Override
    public void onGetWeight(final String String, final boolean isSta) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isSta) {
                    tv_weight.setText("稳定：" + String + "kg");
                } else {
                    tv_weight.setText("未稳定：" + String + "kg");
                }
            }
        });
    }

}
